package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.GxDeptAssess;
import com.ruoyi.system.domain.GxUserAssess;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.GxDeptAssessMapper;
import com.ruoyi.system.mapper.GxUserAssessMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GxUserDeptMapper;
import com.ruoyi.system.domain.GxUserDept;
import com.ruoyi.system.service.IGxUserDeptService;

/**
 * 医生分配Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-01-16
 */
@Service
public class GxUserDeptServiceImpl implements IGxUserDeptService 
{
    @Autowired
    private GxUserDeptMapper gxUserDeptMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private GxDeptAssessMapper deptAssessMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private GxUserAssessMapper userAssessMapper;



    /**
     * 查询医生分配
     * 
     * @param deptId 医生分配主键
     * @return 医生分配
     */
    @Override
    public List<GxUserDept> selectGxUserDeptByDeptId(Long deptId)
    {
        return gxUserDeptMapper.selectGxUserDeptByDeptId(deptId);
    }

    /**
     * 查询医生分配列表
     * 
     * @param gxUserDept 医生分配
     * @return 医生分配
     */
    @Override
    public List<GxUserDept> selectGxUserDeptList(GxUserDept gxUserDept)
    {
        return gxUserDeptMapper.selectGxUserDeptList(gxUserDept);
    }

    /**
     * 新增医生分配
     * 
     * @param gxUserDeptList 医生分配
     * @return 结果
     */
    @Override
    public int insertGxUserDept(List<GxUserDept> gxUserDeptList)
    {
        insertUserRole(gxUserDeptList);
        for (GxUserDept userDept : gxUserDeptList) {
            insertUserDeptAuthAssess(userDept);
        }
        return gxUserDeptMapper.insertGxUserDept(gxUserDeptList);
    }

    public void insertUserDeptAuthAssess(GxUserDept userDept)
    {
        Long deptId = userDept.getDeptId();
        //获取团队的评定权限列表
        List<GxDeptAssess> deptAssesses = deptAssessMapper.selectDeptAssessByDeptId(deptId);
        List<Long> list = new ArrayList<>();
        for (GxDeptAssess gxDeptAssess : deptAssesses) {
            Long assessId = gxDeptAssess.getAssessId();
            list.add(assessId);
        }
        Long userId = userDept.getUserId();
        List<Long> assessIdList = new ArrayList<>();
        GxUserAssess userAssess = new GxUserAssess();
        for (Long assessId : list) {
            userAssess.setUserId(userId);
            userAssess.setAssessId(assessId);
            List<GxUserAssess> gxUserAssesses = userAssessMapper.selectUserAssess(userAssess);
            if (gxUserAssesses.size() == 0) {
                assessIdList.add(assessId);
            }
        }
        Long[] assessIds = assessIdList.toArray(new Long[assessIdList.size()]);
        //添加用户评定权限
        userService.insertUserAssess(userId, assessIds);
    }

    /**
     * 修改医生分配
     * 
     * @param gxUserDept 医生分配
     * @return 结果
     */
    @Override
    public int updateGxUserDept(GxUserDept gxUserDept)
    {
        updateUserRole(gxUserDept);
        return gxUserDeptMapper.updateGxUserDept(gxUserDept);
    }

    /**
     * 批量删除医生分配
     * 
     * @param gxUserDept
     * @return 结果
     */
    @Override
    public int deleteGxUserDeptByUserIds(GxUserDept gxUserDept)
    {
        deleteUserRole(gxUserDept);
        deleteUserAssess(gxUserDept);
        return gxUserDeptMapper.deleteGxUserDeptByUserIds(gxUserDept);
    }

    public void deleteUserAssess(GxUserDept gxUserDept)
    {
        Long userId = gxUserDept.getUserId();
        Long deptId = gxUserDept.getDeptId();
        GxUserDept userDept = new GxUserDept();
        userDept.setUserId(userId);
        List<GxUserDept> gxUserDepts = gxUserDeptMapper.selectGxUserDeptList(userDept);
        List<Long> deptIds = new ArrayList<>();
        for (GxUserDept ud : gxUserDepts) {
            deptIds.add(ud.getDeptId());
        }
        Set<Long> assIds = new HashSet<>();
        for (Long dId : deptIds) {
            if (dId != deptId) {
                List<GxDeptAssess> das = deptAssessMapper.selectDeptAssessByDeptId(dId);
                for (GxDeptAssess da : das) {
                    assIds.add(da.getAssessId());
                }
            }
        }
        List<Long> assessIds = new ArrayList<>(assIds);
        List<GxDeptAssess> deptAssesses = deptAssessMapper.selectDeptAssessByDeptId(deptId);
        GxUserAssess userAssess = new GxUserAssess();
        for (GxDeptAssess deptAssess : deptAssesses) {
            Long assessId = deptAssess.getAssessId();
            if (!assessIds.contains(assessId)){
                userAssess.setUserId(userId);
                userAssess.setAssessId(assessId);
                userAssessMapper.deleteUserAssess(userAssess);
            }
        }
    }

    /**
     * 删除医生分配信息
     * 
     * @param userId 医生分配主键
     * @return 结果
     */
    @Override
    public int deleteGxUserDeptByUserId(Long userId)
    {
        return gxUserDeptMapper.deleteGxUserDeptByUserId(userId);
    }

    /**
     * 新增用户角色信息
     *
     * @param gxUserDeptList 用户对象列表
     */
    public void insertUserRole(List<GxUserDept> gxUserDeptList)
    {
        List<SysUserRole> userRoleList=new ArrayList<>();
        SysUserRole sysUserRole = new SysUserRole();
        for(GxUserDept gud:gxUserDeptList){
            //把未分配角色的医生，添加到list中，批量分配普通角色
            if(userRoleMapper.countUserRoleByUserId(gud.getUserId())==0){
                sysUserRole.setUserId(gud.getUserId());
                sysUserRole.setRoleId(2L);
                userRoleList.add(sysUserRole);
            }
        }
        if(userRoleList.size()>0){
            userRoleMapper.batchUserRole(userRoleList);
        }
    }
    /**
     * 修改用户角色信息
     *
     * @param gxUserDept 用户对象
     */
    public void updateUserRole(GxUserDept gxUserDept)
    {
        SysUserRole sysUserRole = new SysUserRole();
        //医生在团队中为高级角色，同时把用户角色改为高级角色
        if(gxUserDept.getSenior()=='1'){
            sysUserRole.setUserId(gxUserDept.getUserId());
            sysUserRole.setRoleId(5L);
            userRoleMapper.updateUserRole(sysUserRole);
        //普通角色同
        }else if(gxUserDept.getSenior()=='0'){
            sysUserRole.setUserId(gxUserDept.getUserId());
            sysUserRole.setRoleId(2L);
            userRoleMapper.updateUserRole(sysUserRole);
        }
    }

    /**
     * 删除用户角色信息
     * @param gxUserDept
     */
    public void deleteUserRole(GxUserDept gxUserDept)
    {
        SysUserRole sysUserRole = new SysUserRole();
        //如果该医生只在当前这一个团队中，无论是组员还是组长，删除该医生的时候，把他的角色信息也删除，不属于高级角色也不属于普通角色
        if(gxUserDeptMapper.countUserDept(gxUserDept)==0){
            userRoleMapper.deleteUserRoleByUserId(gxUserDept.getUserId());
        }else{
            //如果不止在当前团队，删除组员时，角色信息不受影响，删除组长时，要把该医生对应角色改为组员
            if(gxUserDept.getSenior()=='1'){
                sysUserRole.setUserId(gxUserDept.getUserId());
                sysUserRole.setRoleId(2L);
                userRoleMapper.updateUserRole(sysUserRole);
            }
        }
    }
}
